package www.woniuxy.com.class112.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import org.springframework.stereotype.Service;
import www.woniuxy.com.class112.dao.UserIdentityMapper;
import www.woniuxy.com.class112.entity.UserIdentity;
import www.woniuxy.com.class112.param.AddApplicationParam;
import www.woniuxy.com.class112.service.UserIdentityService;
import www.woniuxy.com.class112.util.AliyunOSSUtil;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class UserIdentityServiceImpl implements UserIdentityService {

    @Resource
    private AliyunOSSUtil ossUtil;
    @Resource
    private UserIdentityMapper userIdentityMapper;
    @Override
    public void addApplicationForm(AddApplicationParam param) throws Exception {
        System.out.println(param);
        UserIdentity userIdentity = BeanUtil.toBean(param, UserIdentity.class);
        String type = FileTypeUtil.getType(param.getImage().getInputStream());
        if (!"jpg".equals(type)&&!"png".equals(type)){
            throw new Exception("文件类型错误");
        }
        String name = param.getImage().getOriginalFilename();
        String substring = name.substring(name.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+ substring;
        String imageUrl = ossUtil.uploadFile(param.getImage().getInputStream(), fileName, "amour520");
        userIdentity.setImage(imageUrl);
        userIdentity.setIdentityType(0L);
        userIdentity.setApplicationTime(new Date());
        userIdentityMapper.insert(userIdentity);
    }
}
